package com.api.gbhg.huangwm.dao;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author	Huangwm
 * @description	预算编制流程节点附件操作扣减、释放收入预算相关数据库操作
 */
public class RevenueBudgetDao {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/dao/RevenueBudgetDao",false);

    private static String YJ ;//科目级别
    private static String XJ ;//科目下级
    static{
        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
    }
    public String czbk_km = "uf_zyczbksr_km" ;//中央财政拨款收入(科目级)
    public String czbk_xj = "uf_zyczbksr_xj" ;//中央财政拨款收入(科目下级)
    public String fzysr = "uf_fzysr" ;//非中央收入本级
    public String fzysrxm = "uf_fzysrxm" ;//非中央收入项目
    public String uf_ysbzyxeshpsj = "uf_ysbzyxeshpsj" ;//预算编制一下二上核批数据

    /**
     * 根据id获取中央下级收入的所有上级id 包含本身
     * @param id
     * @return
     */
    public List<String> getSuperiorId(String id){
        List<String> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = " select sjzxsr from  uf_zyczbksr_xj where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        if (StrUtil.isNotEmpty(rs.getString("sjzxsr"))){
            List<String> sjzxsr = getSuperiorId(rs.getInt("sjzxsr")+"");
            list.addAll(sjzxsr);
        }
        list.add(String.valueOf(id));
        return list;
    }

    /**
     * 根据非中央收入项目id获取所在预算收入分类 以及所属非中央收入表id
     * @param id
     * @return
     */
    public Map<String,String> getFzysrxmFL(String id){
        Map<String,String> fzysrFL = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = " select szfl,yssrfl from uf_fzysrxm where id= "+id;
        rs.execute(sql);
        if(rs.next()){
            fzysrFL.put("szfl",rs.getString("szfl"));
            fzysrFL.put("yssrfl",rs.getString("yssrfl"));
        }
        return fzysrFL;
    }

    /**
     * 根据单位、预算年度和分类找非财政收入分类表id
     * @param dw
     * @param nf
     * @param yssrfl
     * @return
     */
    public String getFzysrid(String dw,String nf,String yssrfl){
        String flid = "";
        RecordSet rs = new RecordSet();
        String sql = " select id from uf_fzysr where dw="+dw+" and nf="+nf+" and yssrfl="+yssrfl;
        rs.execute(sql);
        if(rs.next()){
            flid = Util.null2String(rs.getString("id"));
        }
        return flid;
    }

    /**
     * 根据字段名称和表名查找对应requestid里的主表字段值
     * @param field
     * @param table
     * @param requestid
     * @return
     */
    public String getFieldValue(String field,String table,String requestid){
        String value = "";
        RecordSet rs = new RecordSet();
        String sql = " select "+field+" from "+table+" where requestid="+requestid;
        rs.execute(sql);
        if(rs.next()){
            value = Util.null2String(rs.getString(field));
        }
        return value;
    }

    /**
     * 根据中央科目下级id获取所在科目级id
     * @param kmxjid
     * @return
     */
    public String getZykmidByxjid(String kmxjid){
        String kmid = "";
        RecordSet rs = new RecordSet();
        String sql = " select szgnkm from uf_zyczbksr_xj where id ="+kmxjid;
        rs.execute(sql);
        if(rs.next()){
            kmid = Util.null2String(rs.getString("szgnkm"));
        }
        return kmid;
    }

    /**
     * 根据上年结转核批明细表和流程mainid获取明细数据
     * @param snDetailTable
     * @param mainid
     * @return
     */
    public List<Map<String,String>> getSNDatas(String snDetailTable,String mainid){
        List<Map<String,String>> snjzDatas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select id,srlx,zyczbksr,zcqd,dfczbkxmsnjz,hpje from "+snDetailTable+" where mainid="+mainid;
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("id", rs.getString("id"));//明细表id
            data.put("srlx", rs.getString("srlx"));//收入预算 0和1 代表财政拨款和非财政拨款
            data.put("zyczbksr", Util.null2String(rs.getString("zyczbksr")));//中央财政拨款收入时的财政拨款预算来源
            data.put("zcqd", Util.null2String(rs.getString("zcqd")));//非财政拨款时的非财政拨款预算来源
            data.put("dfczbkxmsnjz", Util.null2String(rs.getString("dfczbkxmsnjz")));//非财政拨款时的非财政拨款项目
            data.put("hpje", rs.getString("hpje"));//核批金额
            snjzDatas.add(data);
        }
        return snjzDatas;
    }

    /**
     * 根据当年预算核批明细表和流程mainid获取明细数据
     * @param dnDetailTable
     * @param mainid
     * @return
     */
    public List<Map<String,String>> getDNDatas(String dnDetailTable,String mainid) {
        List<Map<String,String>> dnysDatas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select id,srlx,zyczbksr,zcqd,dfczbkxmdnys,hpje from "+dnDetailTable+" where mainid="+mainid;
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("id", rs.getString("id"));//明细表id
            data.put("srlx", rs.getString("srlx"));//收入预算 0和1 代表财政拨款和非财政拨款
            data.put("zyczbksr", rs.getString("zyczbksr"));//中央财政拨款收入时的财政拨款预算来源
            data.put("zcqd", rs.getString("zcqd"));//非财政拨款时的非财政拨款预算来源
            data.put("dfczbkxmdnys", rs.getString("dfczbkxmdnys"));//非财政拨款时的非财政拨款项目
            data.put("hpje", rs.getString("hpje"));//核批金额
            dnysDatas.add(data);
        }
        return dnysDatas;
    }

    /**
     * 获取、排序、比较两个Map中相同key对应value值
     * @param hashMap 原数据
     * @param hashMap2 需要比较的数据
     * @return
     */
    public boolean mapCompar(HashMap<String,String> hashMap,HashMap<String,String> hashMap2) {
        boolean isChange = false;
        for (Map.Entry<String, String> entry1 : hashMap.entrySet()) {
            String m1value = entry1.getValue() == null ? "" :  entry1.getValue();
            String m2value = hashMap2.get(entry1.getKey()) == null ? "" :  hashMap2.get(entry1.getKey());
            if (!m1value.equals(m2value)) {
                isChange = true;
            }
            logTool.writeLog("isChange"+isChange);
        }
        return isChange;
    }

    /**
     * 存档核批数据
     * @param mainData
     * @param SNDatas
     * @param DNDatas
     * @return code为1则成功 2为失败
     */
    public Map<String, Object> setPeojectApprovalRecords(Map<String,String> mainData,List<Map<String,String>> SNDatas,List<Map<String,String>> DNDatas){
        Map<String, Object> result = new HashMap<>();
        boolean f1 = true;
        boolean f2 = true;
        boolean f3 = true;

        RecordSet rs = new RecordSet();
        RecordSetTrans rst2 = new RecordSetTrans();
        rst2.setAutoCommit(false);
        RecordSetTrans rst3 = new RecordSetTrans();
        rst3.setAutoCommit(false);
        String sql1 = "insert into uf_ysbzyxeshpsj(lcid,versionStr,xmmc,ysnd,sqze,hpze)values(?,?,?,?,?,?)";
        String sql2 = "insert into uf_ysbzyxeshpsj_dt1(mainid,mxid,srlx,zyczbksr,zcqd,dfczbkxmsnjz,hpje)values(?,?,?,?,?,?,?)";
        String sql3 = "insert into uf_ysbzyxeshpsj_dt2(mainid,mxid,srlx,zyczbksr,zcqd,dfczbkxmdnys,hpje)values(?,?,?,?,?,?,?)";
        String ysnd = mainData.get("ysnd");
        String lcid = mainData.get("lcid");
        RecordSet del = new RecordSet();
        del.execute("delete from uf_ysbzyxeshpsj where lcid="+lcid);
        del.execute("delete from uf_ysbzyxeshpsj_dt1 where mainid="+lcid);
        del.execute("delete from uf_ysbzyxeshpsj_dt2 where mainid="+lcid);

        String xmmc = mainData.get("xmmc");
        String sqze = mainData.get("sqze");
        String hpze = mainData.get("hpze");
        String version = mainData.get("version");
        Object[] sql1Params = {
                lcid,version,xmmc,ysnd,sqze,hpze
        };
        List<List<Object>> sql2Params = new ArrayList<>();//上年结转
        List<List<Object>> sql3Params = new ArrayList<>();//当年预算
        if(SNDatas.size()>0){
            for(int i=0;i<SNDatas.size();i++) {
                Map<String, String> data = SNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmsnjz = data.get("dfczbkxmsnjz");
                String hpje = data.get("hpje");

                List<Object> params2 = new ArrayList<>();
                params2.add(lcid);
                params2.add(id);
                params2.add(srlx);
                params2.add(zyczbksr);
                params2.add(zcqd);
                params2.add(dfczbkxmsnjz);
                params2.add(hpje);
                sql2Params.add(params2);
            }
            try {
                rst2.executeBatchSql(sql2,sql2Params);
            }catch (Exception e){
                f2 = false;
                rst2.rollback();
            }
        }
        if(DNDatas.size()>0){
            for(int i=0;i<DNDatas.size();i++) {
                Map<String, String> data = DNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmdnys = data.get("dfczbkxmdnys");
                String hpje = data.get("hpje");

                List<Object> params3 = new ArrayList<>();
                params3.add(lcid);
                params3.add(id);
                params3.add(srlx);
                params3.add(zyczbksr);
                params3.add(zcqd);
                params3.add(dfczbkxmdnys);
                params3.add(hpje);
                sql3Params.add(params3);
            }
            try {
                rst3.executeBatchSql(sql3,sql3Params);
            }catch (Exception e){
                f3 = false;
                rst3.rollback();
            }
        }
        f1 = rs.executeUpdate(sql1,sql1Params);

        if(f1 && f2 && f3){
            rst2.commit();
            rst3.commit();
            result.put("code", "1");
        }else{
            rst2.rollback();
            rst3.rollback();
            result.put("code", "2");
        }
        result.put("f1", f1);
        result.put("f2", f2);
        result.put("f3", f3);
        return result;
    }

    /**
     * 根据上年结转he当年预算核批内容扣减核批的收入预算
     * @param SNDatas
     * @param DNDatas
     * @param dw
     * @param ysnd
     * @return code为1则成功 2为失败
     */
    public Map<String, Object> RevenueBudgetDeduction(List<Map<String,String>> SNDatas,List<Map<String,String>> DNDatas,Integer dw,String ysnd) {
        RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
        Map<String, Object> apiResult = new HashMap<>();

        /**
         * 开始执行收入预算扣减逻辑
         */
        //先扣减上年结转
        List<List<Object>> SNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> SNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateSNKM = "update "+budgetUtils.czbk_km+" set snjzhpje=isnull(snjzhpje,0)+?,snjzhpsyje=isnull(snjzhpsyje,0)+?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)+?,yqndyjjzsyje=yqndyjjzsyje-? where id =?";
        String updateSNKMXJ = "update "+budgetUtils.czbk_xj+" set snjzhpje=isnull(snjzhpje,0)+?,snjzhpsyje=isnull(snjzhpsyje,0)+?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)+?,yqndyjjzsyje=yqndyjjzsyje-? where id =?";

        List<List<Object>> SNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> SNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateSNFZYSR = "update "+budgetUtils.fzysr+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)+?,yqndyjjzsyje=yqndyjjzsyje-?,snjzhpje=isnull(snjzhpje,0)+?,snjzhpsyje=isnull(snjzhpsyje,0)+? where id=?";
        String updateSNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)+?,yqndyjjzsyje=yqndyjjzsyje-?,snjzhpje=isnull(snjzhpje,0)+?,snjzhpsyje=isnull(snjzhpsyje,0)+? where id=?";

        if(SNDatas.size()>0){
            logTool.writeLog("--------上年结转--------");
            RecordSetTrans operation_SN = new RecordSetTrans();
            operation_SN.setAutoCommit(false);
            for(int i=0;i<SNDatas.size();i++){
                Map<String,String> data = SNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmsnjz = data.get("dfczbkxmsnjz");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        //System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            SNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmsnjz);
                        SNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmsnjz).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(SNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(SNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(SNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(SNparams_fczbkxm));
            logTool.writeLog("--------上年结转--------");
        }


        //再扣减当年预算
        List<List<Object>> DNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> DNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateDNKM = "update "+budgetUtils.czbk_km+" set yhpje=isnull(yhpje,0)+?,hpsyje=ISNULL(hpsyje,0)+?,yjsrsy=yjsrsy-? where id =?";
        String updateDNKMXJ = "update "+budgetUtils.czbk_xj+" set yhpje=isnull(yhpje,0)+?,yhpsy=ISNULL(yhpsy,0)+?,yjsrsy=yjsrsy-? where id =?";

        List<List<Object>> DNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> DNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateDNFZYSR = "update "+budgetUtils.fzysr+" set yhpje=isnull(yhpje,0)+?,hpsyje=isnull(hpsyje,0)+?,yjsrsy=yjsrsy-? where id=?";
        String updateDNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yhpje=isnull(yhpje,0)+?,hpsyje=isnull(hpsyje,0)+?,yjsrsy=yjsrsy-? where id=?";

        if(DNDatas.size()>0){
            logTool.writeLog("--------当年预算--------");
            for(int i=0;i<DNDatas.size();i++){
                Map<String,String> data = DNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmdnys = data.get("dfczbkxmdnys");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        // System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            DNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmdnys);
                        DNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmdnys).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(DNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(DNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(DNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(DNparams_fczbkxm));
            logTool.writeLog("--------当年预算--------");
        }

        RecordSetTrans operation = new RecordSetTrans();
        operation.setAutoCommit(false);
        boolean f1 = true,f2 = true,f3 = true,f4 = true;
        boolean f5 = true,f6 = true,f7 = true,f8 = true;
        //更新当年财政拨款科目金额
        try {
            operation.executeBatchSql(updateDNKM, DNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f1 = false;
            logTool.writeLog("扣减当年财政拨款科目金额SQL执行异常");
        }
        //更新当年财政拨款科目下级金额
        try {
            operation.executeBatchSql(updateDNKMXJ, DNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f2 = false;
            logTool.writeLog("扣减当年财政拨款科目下级金额SQL执行异常");
        }
        //更新当年非中央收入分类金额
        try {
            operation.executeBatchSql(updateDNFZYSR, DNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f3 = false;
            logTool.writeLog("扣减当年非中央收入分类金额SQL执行异常");
        }
        //更新当年非中央项目收入金额
        try {
            operation.executeBatchSql(updateDNFZYSRXM, DNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f4 = false;
            logTool.writeLog("扣减当年非中央项目收入金额SQL执行异常");
        }

        //更新上年结转财政拨款科目收入金额
        try {
            operation.executeBatchSql(updateSNKM, SNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f5 = false;
            logTool.writeLog("扣减上年结转财政拨款科目收入金额SQL执行异常");
        }
        //更新上年结转财政拨款科目下级收入金额
        try {
            operation.executeBatchSql(updateSNKMXJ, SNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f6 = false;
            logTool.writeLog("扣减上年结转财政拨款科目下级收入金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入分类金额
        try {
            operation.executeBatchSql(updateSNFZYSR, SNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f7 = false;
            logTool.writeLog("扣减上年结转非财政拨款收入分类金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入项目金额
        try {
            operation.executeBatchSql(updateSNFZYSRXM, SNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f8 = false;
            logTool.writeLog("扣减上年结转非财政拨款收入项目金额SQL执行异常");
        }

        if(f1 && f2 && f3 && f4 && f5 && f6 && f7 && f8){
            operation.commit();
            logTool.writeLog("SUCCESS 收入预算金额扣减成功");
            apiResult.put("msg","SUCCESS 收入预算金额扣减成功");
            apiResult.put("code", "1");
        }else{
            operation.rollback();
            apiResult.put("msg","执行扣减SQL异常 具体请查看各执行结果");
            apiResult.put("code", "2");
            if(!f1){
                apiResult.put("f1","扣减当年财政拨款科目金额SQL执行异常");
            }
            if(!f2){
                apiResult.put("f2","扣减当年财政拨款科目下级金额SQL执行异常");
            }
            if(!f3){
                apiResult.put("f3","扣减当年非中央收入本级金额SQL执行异常");
            }
            if(!f4){
                apiResult.put("f4","扣减当年非中央项目收入金额SQL执行异常");
            }
            if(!f5){
                apiResult.put("f5","扣减上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f6){
                apiResult.put("f6","扣减上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f7){
                apiResult.put("f7","扣减上年结转非财政拨款收入分类金额SQL执行异常");
            }
            if(!f8){
                apiResult.put("f8","扣减上年结转非财政拨款收入项目金额SQL执行异常");
            }
        }
        return apiResult;
    }

    /**
     * 根据requestid释放核批的收入预算
     * @param requestid
     * @return code为1则成功 2为失败
     */
    public Map<String, Object> RevenueBudgetReturn(String requestid) {
        RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
        logTool.writeLog("===== RevenueBudgetReturn 释放收入预算=====");
        Map<String, Object> apiResult = new HashMap<String, Object>();
        User user = new User(10260);
        RequestInfo requestInfo = new RequestInfo(Integer.parseInt(requestid),user);

        logTool.writeLog("workflowid="+requestInfo.getWorkflowid()+"  requestid="+requestInfo.getRequestid());
        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
        logTool.writeLog("财政拨款核批层级 YJ="+YJ+"  XJ="+XJ);
        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id
        //System.err.println(mainid+" 核批明细 "+flowDetailTableNames);

        List<Map<String,String>> getSNDatas = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas = new ArrayList<>();//当年预算
        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                apiResult.put("code", "2");
                apiResult.put("msg", "获取预算年度为空 ");
                return  apiResult;
            }
        }catch (Exception e){
            apiResult.put("code", "2");
            apiResult.put("msg", "获取预算年度异常 "+e.getMessage());
            return  apiResult;
        }
        try {
            getSNDatas = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转
            getDNDatas = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算
            logTool.writeLog(" 上年结转 "+getSNDatas);
            logTool.writeLog(" 当年预算 "+getDNDatas);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.put("code", "2");
            apiResult.put("msg", "获取核批结果异常 "+e.getMessage());
            return  apiResult;
        }

        /**
         * 开始执行收入预算释放逻辑
         */
        //先释放上年结转
        List<List<Object>> SNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> SNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateSNKM = "update "+budgetUtils.czbk_km+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";
        String updateSNKMXJ = "update "+budgetUtils.czbk_xj+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";

        List<List<Object>> SNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> SNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateSNFZYSR = "update "+budgetUtils.fzysr+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";
        String updateSNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";

        if(getSNDatas.size()>0){
            logTool.writeLog("--------上年结转--------");
            RecordSetTrans operation_SN = new RecordSetTrans();
            operation_SN.setAutoCommit(false);
            for(int i=0;i<getSNDatas.size();i++){
                Map<String,String> data = getSNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmsnjz = data.get("dfczbkxmsnjz");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        //System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            SNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmsnjz);
                        SNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmsnjz).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(SNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(SNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(SNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(SNparams_fczbkxm));
            logTool.writeLog("--------上年结转--------");
        }

        //再释放当年预算
        List<List<Object>> DNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> DNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateDNKM = "update "+budgetUtils.czbk_km+" set yhpje=isnull(yhpje,0)-?,hpsyje=ISNULL(hpsyje,0)-?,yjsrsy=yjsrsy+? where id =?";
        String updateDNKMXJ = "update "+budgetUtils.czbk_xj+" set yhpje=isnull(yhpje,0)-?,yhpsy=ISNULL(yhpsy,0)-?,yjsrsy=yjsrsy+? where id =?";

        List<List<Object>> DNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> DNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateDNFZYSR = "update "+budgetUtils.fzysr+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";
        String updateDNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";

        if(getDNDatas.size()>0){
            logTool.writeLog("--------当年预算--------");
            for(int i=0;i<getDNDatas.size();i++){
                Map<String,String> data = getDNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmdnys = data.get("dfczbkxmdnys");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        // System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            DNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmdnys);
                        DNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmdnys).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(DNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(DNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(DNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(DNparams_fczbkxm));
            logTool.writeLog("--------当年预算--------");
        }

        RecordSetTrans operation = new RecordSetTrans();
        operation.setAutoCommit(false);
        boolean f1 = true,f2 = true,f3 = true,f4 = true;
        boolean f5 = true,f6 = true,f7 = true,f8 = true;
        //更新当年财政拨款科目金额
        try {
            operation.executeBatchSql(updateDNKM, DNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f1 = false;
            logTool.writeLog("释放当年财政拨款科目金额SQL执行异常");
        }
        //更新当年财政拨款科目下级金额
        try {
            operation.executeBatchSql(updateDNKMXJ, DNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f2 = false;
            logTool.writeLog("释放当年财政拨款科目下级金额SQL执行异常");
        }
        //更新当年非中央收入分类金额
        try {
            operation.executeBatchSql(updateDNFZYSR, DNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f3 = false;
            logTool.writeLog("释放当年非中央收入分类金额SQL执行异常");
        }
        //更新当年非中央项目收入金额
        try {
            operation.executeBatchSql(updateDNFZYSRXM, DNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f4 = false;
            logTool.writeLog("释放当年非中央项目收入金额SQL执行异常");
        }

        //更新上年结转财政拨款科目收入金额
        try {
            operation.executeBatchSql(updateSNKM, SNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f5 = false;
            logTool.writeLog("释放上年结转财政拨款科目收入金额SQL执行异常");
        }
        //更新上年结转财政拨款科目下级收入金额
        try {
            operation.executeBatchSql(updateSNKMXJ, SNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f6 = false;
            logTool.writeLog("释放上年结转财政拨款科目下级收入金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入分类金额
        try {
            operation.executeBatchSql(updateSNFZYSR, SNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f7 = false;
            logTool.writeLog("释放上年结转非财政拨款收入分类金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入项目金额
        try {
            operation.executeBatchSql(updateSNFZYSRXM, SNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f8 = false;
            logTool.writeLog("释放上年结转非财政拨款收入项目金额SQL执行异常");
        }

        if(f1 && f2 && f3 && f4 && f5 && f6 && f7 && f8){
            operation.commit();
            logTool.writeLog("SUCCESS 收入预算金额释放成功");
            apiResult.put("msg","SUCCESS 收入预算金额释放成功");
            apiResult.put("code", "1");
        }else{
            operation.rollback();
            apiResult.put("msg","执行释放SQL异常 具体请查看各执行结果");
            apiResult.put("code", "2");
            if(!f1){
                apiResult.put("f1","释放当年财政拨款科目金额SQL执行异常");
            }
            if(!f2){
                apiResult.put("f2","释放当年财政拨款科目下级金额SQL执行异常");
            }
            if(!f3){
                apiResult.put("f3","释放当年非中央收入本级金额SQL执行异常");
            }
            if(!f4){
                apiResult.put("f4","释放当年非中央项目收入金额SQL执行异常");
            }
            if(!f5){
                apiResult.put("f5","释放上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f6){
                apiResult.put("f6","释放上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f7){
                apiResult.put("f7","释放上年结转非财政拨款收入分类金额SQL执行异常");
            }
            if(!f8){
                apiResult.put("f8","释放上年结转非财政拨款收入项目金额SQL执行异常");
            }
        }
        return apiResult;
    }

    /**
     * 根据requestid获取存在uf_ysbzyxeshpsj_dt1的上年结转核批数据
     * @param requestid
     * @return
     */
    public List<Map<String,String>> getSNDatas_02(String requestid){
        List<Map<String,String>> snjzDatas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select mxid,srlx,zyczbksr,zcqd,dfczbkxmsnjz,hpje from uf_ysbzyxeshpsj_dt1 where mainid="+requestid;
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("id", rs.getString("mxid"));//明细表id
            data.put("srlx", rs.getString("srlx"));//收入预算 0和1 代表财政拨款和非财政拨款
            data.put("zyczbksr", Util.null2String(rs.getString("zyczbksr")));//中央财政拨款收入时的财政拨款预算来源
            data.put("zcqd", Util.null2String(rs.getString("zcqd")));//非财政拨款时的非财政拨款预算来源
            data.put("dfczbkxmsnjz", Util.null2String(rs.getString("dfczbkxmsnjz")));//非财政拨款时的非财政拨款项目
            data.put("hpje", rs.getString("hpje"));//核批金额
            snjzDatas.add(data);
        }
        return snjzDatas;
    }

    /**
     * 根据requestid获取存在uf_ysbzyxeshpsj_dt2的当年预算核批数据
     * @param requestid
     * @return
     */
    public List<Map<String,String>> getDNDatas_02(String requestid) {
        List<Map<String,String>> dnysDatas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select mxid,srlx,zyczbksr,zcqd,dfczbkxmdnys,hpje from uf_ysbzyxeshpsj_dt2 where mainid="+requestid;
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("id", rs.getString("mxid"));//明细表id
            data.put("srlx", rs.getString("srlx"));//收入预算 0和1 代表财政拨款和非财政拨款
            data.put("zyczbksr", rs.getString("zyczbksr"));//中央财政拨款收入时的财政拨款预算来源
            data.put("zcqd", rs.getString("zcqd"));//非财政拨款时的非财政拨款预算来源
            data.put("dfczbkxmdnys", rs.getString("dfczbkxmdnys"));//非财政拨款时的非财政拨款项目
            data.put("hpje", rs.getString("hpje"));//核批金额
            dnysDatas.add(data);
        }
        return dnysDatas;
    }

    /**
     * 根据上年结转和当年预算的核批数据释放收入预算
     * @param SNDatas
     * @param DNDatas
     * @param dw
     * @param ysnd
     * @return code为1则成功 2为失败
     */
    public Map<String, Object> RevenueBudgetReturnV2(List<Map<String,String>> SNDatas,List<Map<String,String>> DNDatas,Integer dw,String ysnd) {
        RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
        Map<String, Object> apiResult = new HashMap<String, Object>();
        logTool.writeLog("===== RevenueBudgetReturnV2 释放收入预算=====");
        /**
         * 开始执行收入预算释放逻辑
         */
        //先释放上年结转
        List<List<Object>> SNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> SNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateSNKM = "update "+budgetUtils.czbk_km+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";
        String updateSNKMXJ = "update "+budgetUtils.czbk_xj+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";

        List<List<Object>> SNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> SNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateSNFZYSR = "update "+budgetUtils.fzysr+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";
        String updateSNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";

        if(SNDatas.size()>0){
            logTool.writeLog("--------上年结转--------");
            RecordSetTrans operation_SN = new RecordSetTrans();
            operation_SN.setAutoCommit(false);
            for(int i=0;i<SNDatas.size();i++){
                Map<String,String> data = SNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmsnjz = data.get("dfczbkxmsnjz");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        //System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            SNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmsnjz);
                        SNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmsnjz).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(SNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(SNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(SNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(SNparams_fczbkxm));
            logTool.writeLog("--------上年结转--------");
        }

        //再释放当年预算
        List<List<Object>> DNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> DNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateDNKM = "update "+budgetUtils.czbk_km+" set yhpje=isnull(yhpje,0)-?,hpsyje=ISNULL(hpsyje,0)-?,yjsrsy=yjsrsy+? where id =?";
        String updateDNKMXJ = "update "+budgetUtils.czbk_xj+" set yhpje=isnull(yhpje,0)-?,yhpsy=ISNULL(yhpsy,0)-?,yjsrsy=yjsrsy+? where id =?";

        List<List<Object>> DNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> DNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateDNFZYSR = "update "+budgetUtils.fzysr+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";
        String updateDNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";

        if(DNDatas.size()>0){
            logTool.writeLog("--------当年预算--------");
            for(int i=0;i<DNDatas.size();i++){
                Map<String,String> data = DNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmdnys = data.get("dfczbkxmdnys");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        // System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            DNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmdnys);
                        DNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmdnys).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("财政拨款科目级》"+ JSONObject.toJSONString(DNparams_czbkkm));
            logTool.writeLog("财政拨款科目下级》"+ JSONObject.toJSONString(DNparams_czbkkmxj));
            logTool.writeLog("非财政拨款分类级》"+ JSONObject.toJSONString(DNparams_fczbk));
            logTool.writeLog("非财政拨款项目级》"+ JSONObject.toJSONString(DNparams_fczbkxm));
            logTool.writeLog("--------当年预算--------");
        }

        RecordSetTrans operation = new RecordSetTrans();
        operation.setAutoCommit(false);
        boolean f1 = true,f2 = true,f3 = true,f4 = true;
        boolean f5 = true,f6 = true,f7 = true,f8 = true;
        //更新当年财政拨款科目金额
        try {
            operation.executeBatchSql(updateDNKM, DNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f1 = false;
            logTool.writeLog("释放当年财政拨款科目金额SQL执行异常");
        }
        //更新当年财政拨款科目下级金额
        try {
            operation.executeBatchSql(updateDNKMXJ, DNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f2 = false;
            logTool.writeLog("释放当年财政拨款科目下级金额SQL执行异常");
        }
        //更新当年非中央收入分类金额
        try {
            operation.executeBatchSql(updateDNFZYSR, DNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f3 = false;
            logTool.writeLog("释放当年非中央收入分类金额SQL执行异常");
        }
        //更新当年非中央项目收入金额
        try {
            operation.executeBatchSql(updateDNFZYSRXM, DNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f4 = false;
            logTool.writeLog("释放当年非中央项目收入金额SQL执行异常");
        }

        //更新上年结转财政拨款科目收入金额
        try {
            operation.executeBatchSql(updateSNKM, SNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f5 = false;
            logTool.writeLog("释放上年结转财政拨款科目收入金额SQL执行异常");
        }
        //更新上年结转财政拨款科目下级收入金额
        try {
            operation.executeBatchSql(updateSNKMXJ, SNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f6 = false;
            logTool.writeLog("释放上年结转财政拨款科目下级收入金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入分类金额
        try {
            operation.executeBatchSql(updateSNFZYSR, SNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f7 = false;
            logTool.writeLog("释放上年结转非财政拨款收入分类金额SQL执行异常");
        }
        //更新上年结转非财政拨款收入项目金额
        try {
            operation.executeBatchSql(updateSNFZYSRXM, SNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f8 = false;
            logTool.writeLog("释放上年结转非财政拨款收入项目金额SQL执行异常");
        }

        if(f1 && f2 && f3 && f4 && f5 && f6 && f7 && f8){
            operation.commit();
            logTool.writeLog("SUCCESS 收入预算金额释放成功");
            apiResult.put("msg","SUCCESS 收入预算金额释放成功");
            apiResult.put("code", "1");
        }else{
            operation.rollback();
            apiResult.put("msg","执行释放SQL异常 具体请查看各执行结果");
            apiResult.put("code", "2");
            if(!f1){
                apiResult.put("f1","释放当年财政拨款科目金额SQL执行异常");
            }
            if(!f2){
                apiResult.put("f2","释放当年财政拨款科目下级金额SQL执行异常");
            }
            if(!f3){
                apiResult.put("f3","释放当年非中央收入本级金额SQL执行异常");
            }
            if(!f4){
                apiResult.put("f4","释放当年非中央项目收入金额SQL执行异常");
            }
            if(!f5){
                apiResult.put("f5","释放上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f6){
                apiResult.put("f6","释放上年结转非中央项目收入金额SQL执行异常");
            }
            if(!f7){
                apiResult.put("f7","释放上年结转非财政拨款收入分类金额SQL执行异常");
            }
            if(!f8){
                apiResult.put("f8","释放上年结转非财政拨款收入项目金额SQL执行异常");
            }
        }
        return apiResult;
    }
}
